package com.xuanyuan.material

import android.annotation.SuppressLint
import android.graphics.Color
import android.graphics.Paint
import android.os.Bundle
import android.view.ViewGroup
import android.widget.TextView
import androidx.core.content.ContextCompat
import com.google.android.material.imageview.ShapeableImageView
import com.google.android.material.shape.EdgeTreatment
import com.google.android.material.shape.MarkerEdgeTreatment
import com.google.android.material.shape.MaterialShapeDrawable
import com.google.android.material.shape.OffsetEdgeTreatment
import com.google.android.material.shape.RoundedCornerTreatment
import com.google.android.material.shape.ShapeAppearanceModel
import com.google.android.material.shape.ShapePath
import com.google.android.material.shape.TriangleEdgeTreatment
import com.xuanyuan.base.BaseActivity
import com.xuanyuan.base.utils.dp
import com.xuanyuan.material.databinding.ActivityMaterialShapeDrawableBinding


/**
 * FileName: ShapeableImageView
 * @author ken.luo
 * Date: 2022/6/9 15:06
 * Description: 继承自ImageView，可以为image添加描边大小、颜色，以及圆角、裁切等，
 * History:
 * 创建者 : ken.luo
 * <author>     <time>     <version>      <desc>
 * 作者姓名      修改时间      版本号          描述
</desc></version></time></author> */

class MaterialShapeDrawableActivity : BaseActivity<ActivityMaterialShapeDrawableBinding>() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        setCorner()

        // 进行Edge的设置展示
        setEdges1()
        setEdges2()
        setEdges3()
        setEdges4()
        setShadow()

    }


    override fun initLayout(): Int {
        return R.layout.activity_material_shape_drawable
    }

    override fun afterInit() {
        val shape: ShapeableImageView
        val model: ShapeAppearanceModel
        val drawable: MaterialShapeDrawable
        val textfsf: TextView

    }

    /**
     * 设置角样式
     */
    private fun setCorner() {
        // 设置边角类型和边角size
        val shapePathModel = ShapeAppearanceModel.builder().setAllCorners(RoundedCornerTreatment())
            .setAllCornerSizes(20.dp).build()
        // 等效的另一种构造方式
//        val shapePathModel2 = ShapeAppearanceModel.builder().setAllCorners(CornerFamily.ROUNDED, 10f).build()
        val backgroundDrawable =
            MaterialShapeDrawable(shapePathModel)
                .apply {
                    setTint(Color.parseColor("#DDBBBB"))
                    paintStyle = Paint.Style.FILL
                }
        // 使用
        mBinding.tvTest.background = backgroundDrawable
    }

    /**
     * 设置Edges 边缘
     */
    private fun setEdges1() {
        val shapePathModel = ShapeAppearanceModel.builder().setAllCorners(RoundedCornerTreatment())
            .setAllCornerSizes(20.dp)
            //直线边框
            .setAllEdges(EdgeTreatment())
            // 会在中间形成间隙
//            .setAllEdges(MarkerEdgeTreatment(10.dp))
//            //进行边缘的设置处理,使用三角形设置,第二个参数使用的inside=true,如果使用false,则需要指定parent view的clipChildren属性为false
//            .setAllEdges(TriangleEdgeTreatment(10.dp, true))
            .build()

        val backgroundDrawable = MaterialShapeDrawable(shapePathModel).apply {
            // 设置背景
            setTint(Color.parseColor("#bebebe"))
            // 设置绘制style
            paintStyle = Paint.Style.FILL_AND_STROKE
            // 设置边框宽
            strokeWidth = 2.dp
        }
        mBinding.tvEdge1.background = backgroundDrawable
    }

    /**
     * 设置Edges 边缘
     */
    private fun setEdges2() {
        val shapePathModel = ShapeAppearanceModel.builder().setAllCorners(RoundedCornerTreatment())
            .setAllCornerSizes(20.dp)
            //直线边框
//            .setAllEdges(EdgeTreatment())
            // 会在中间形成间隙
            .setAllEdges(MarkerEdgeTreatment(10.dp))
            //进行边缘的设置处理,使用三角形设置,第二个参数使用的inside=true,如果使用false,则需要指定parent view的clipChildren属性为false
//            .setAllEdges(TriangleEdgeTreatment(10.dp, true))
            .build()

        val backgroundDrawable = MaterialShapeDrawable(shapePathModel).apply {
            // 设置背景
            setTint(Color.parseColor("#bebebe"))
            // 设置绘制style
            paintStyle = Paint.Style.FILL_AND_STROKE
            // 设置边框宽
            strokeWidth = 2.dp
        }
        mBinding.tvEdge2.background = backgroundDrawable
    }

    /**
     * 设置Edges 边缘
     */
    private fun setEdges3() {
        val shapePathModel = ShapeAppearanceModel.builder().setAllCorners(RoundedCornerTreatment())
            .setAllCornerSizes(20.dp)
            // 代码设置 聊天框效果
            .setAllEdges(object : TriangleEdgeTreatment(10.dp, true) {
                override fun getEdgePath(
                    length: Float,
                    center: Float,
                    interpolation: Float,
                    shapePath: ShapePath
                ) {
                    super.getEdgePath(length, 35f, interpolation, shapePath)
                }
            })
            .build()

        val backgroundDrawable = MaterialShapeDrawable(shapePathModel).apply {
            // 设置背景
            setTint(Color.parseColor("#bebebe"))
            // 设置绘制style
            paintStyle = Paint.Style.FILL_AND_STROKE
            // 设置边框宽
            strokeWidth = 2.dp
        }
        // 不限制子view在其范围内,如果TriangleEdgeTreatment 设置的参数为false的话，需要进行如下设置
//        (mBinding.tvEdge3.parent as ViewGroup).clipChildren=false
        mBinding.tvEdge3.background = backgroundDrawable
    }

    /**
     * 设置Edges 边缘
     */
    private fun setEdges4() {
        val shapePathModel = ShapeAppearanceModel.builder()
            .setAllCorners(RoundedCornerTreatment())
            .setAllCornerSizes(20.dp)
            .setAllEdges(OffsetEdgeTreatment(MarkerEdgeTreatment(10.dp), 10.dp))
            .build()

        val backgroundDrawable = MaterialShapeDrawable(shapePathModel).apply {
            // 设置背景
            setTint(Color.parseColor("#bebebe"))
            // 设置绘制style
            paintStyle = Paint.Style.FILL_AND_STROKE
            // 设置边框宽
            strokeWidth = 2.dp
            strokeColor =
                ContextCompat.getColorStateList(this@MaterialShapeDrawableActivity, R.color.red)
        }
        mBinding.tvEdge4.background = backgroundDrawable
    }


    /**
     * 设置控件阴影
     */
    @SuppressLint("RestrictedApi")
    private fun setShadow() {
        val shapePathModel = ShapeAppearanceModel.builder()
            .setAllCorners(RoundedCornerTreatment())
            .setAllCornerSizes(16.dp)
            .build()

        val backgroundDrawable = MaterialShapeDrawable(shapePathModel).apply {
//          setTint(Color.parseColor("#FF3D3A3A"))
            setTint(resources.getColor(R.color.red))
            paintStyle = Paint.Style.FILL

            // 始终绘制假阴影，从不绘制原生高程阴影。如果路径可能是凹形的，这将防止阴影突然以本机方式渲染。使用的是fake shadow
            shadowCompatibilityMode = MaterialShapeDrawable.SHADOW_COMPAT_MODE_ALWAYS
            // elevation 阴影绘制方式
//            shadowCompatibilityMode = MaterialShapeDrawable.SHADOW_COMPAT_MODE_DEFAULT
            // 不显示阴影
//            shadowCompatibilityMode = MaterialShapeDrawable.SHADOW_COMPAT_MODE_NEVER
            initializeElevationOverlay(this@MaterialShapeDrawableActivity)
            shadowRadius = 8.dp.toInt()
//            setShadowColor(Color.parseColor("#7A8F61"))
            setShadowColor(resources.getColor(R.color.color5))
            shadowVerticalOffset = 4.dp.toInt()
        }
//        mBinding.tvShadow?.background = backgroundDrawable

        // 该步骤不可少
        (mBinding.tvShadow?.parent as? ViewGroup)?.clipChildren = false
        mBinding.tvShadow?.background = backgroundDrawable


    }


}


